#ifndef COMMON_H
#define COMMON_H

#include <iostream>
#include <sstream>
#include <fstream>
#include <sys/ioctl.h>
#include <cstdio>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <netdb.h>
#include <vector>
#include <queue>
#include <list>
#include <algorithm>
#include <semaphore.h>
#include <iomanip>
#include "assert.h"

using namespace std;

#ifdef DEBUG
#define DBG_PRINTF(x) printf x 
#else
#define DBG_PRINTF(x)
#endif

// [*] use printf() instead of cout() due to thread safety (printf prints all in one go, cout may consist of multiple atomic operations)
// [*] double check all sockets closed properly as needed

//const int chunkSize = 65536;
const int chunkSize = 6553;
const int bufSize = 9001;
const int maxPeers  = 6;
const int maxFiles  = 100;    // Cheesy, but allows us to do a simple Status class
const int PORT = 10021;     // fixed port number if ran on seperate ecelinux machines.

const int storageThreshold = 80000;

const int maxWorkers = maxPeers;
//const int maxWorkers = 2;

const int tableWidth = 18;
const int showWidth = 18;

// Return codes
// This is a defined set of return codes.
// Using enum here is a bad idea, unless you spell out the values, since it is
// shared with code you don't write.

const int SAFE                   =  0; // Everything good

const int exitListener           =  0x0001; // ExitListener
const int exitWorker             =  0x0002; // ExitWorker
const int errUnknownWarning      =  0x0003; // Unknown warning
const int errPeerNotFound        =  0x0101; // Cannot find some peer; warning, since others may be connectable
const int errAlreadyJoined       =  0x0102; // Already joined the network
const int errNotConnected        =  0x0103; // Currently not connected to network
const int errInvalidMessage      =  0x0201; // Received message of invalid/unknown type
const int errInvalidJob          =  0x0301; // Received job of invalid/unknown type
const int errFileAlreadyExist    =  0x0401; // File already exist on the network
const int errNoChunkToShare      =  0x0402; // No chunk to share at the moment
const int warnFileNotOpen        =  0x0403; // Cannot modify file since file is not opened

const int errUnknownFatal        = -0x0001; // Unknown error
const int errCannotConnect       = -0x0101; // Cannot connect to anything; fatal error
const int errNoPeersFound        = -0x0201; // Cannot find any peer (e.g., no peers in a peer file); fatal
const int errNoFileFound         = -0x0301; // File does not exist
const int errNoDirectoryFound    = -0x0302; // Directory does not exist
const int errChunkNotFound       = -0x0303; // Chunk does not exist for a file
const int errVersionNotFound     = -0x0304; // Version does not exist for a file
const int errVersionNotFound     = -0x0305; // File is not available
const int errSocketNotCreated    = -0x0401; // Failed to create a socket

// please add as necessary

#endif
